package com.zhongkun.datahome.service.service.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhongkun.datahome.common.exception.ServiceException;
import com.zhongkun.datahome.core.domain.vo.UserBaseVo;
import com.zhongkun.datahome.core.mapper.SysUserMapper;
import com.zhongkun.datahome.service.domain.Project;
import com.zhongkun.datahome.service.repository.ProjectRepository;
import com.zhongkun.datahome.service.req.qry.project.ProjectQry;
import com.zhongkun.datahome.service.res.PageVo;
import com.zhongkun.datahome.service.res.project.ProjectVo;
import com.zhongkun.datahome.service.service.IProjectService;
import com.zhongkun.datahome.service.utils.StreamUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-14
 */
@Service
public class ProjectServiceImpl implements IProjectService {
    @Resource
    private ProjectRepository projectRepository;

    @Resource
    private SysUserMapper sysUserMapper;


    @Override
    public PageVo<ProjectVo> page(ProjectQry qry) {
        IPage<Project> page = projectRepository.page(qry);
        if (CollectionUtils.isEmpty(page.getRecords())) {
            return PageVo.emptyPage(qry.getPageNo(), qry.getPageSize());
        }

        Set<Long> tenantUserIds = new HashSet<>();
        page.getRecords().forEach(item -> {
            tenantUserIds.add(item.getCreateBy());
            tenantUserIds.add(item.getUpdateBy());
        });

        Map<Long, String> nameMap = StreamUtil.listToMap(sysUserMapper.getNickNameMap(tenantUserIds), UserBaseVo::getTenantUserId, UserBaseVo::getNickName);
        List<ProjectVo> projectVoList = page.getRecords().stream()
                .map(item -> ProjectVo.convert(item, nameMap))
                .collect(Collectors.toList());

        return new PageVo<>(projectVoList, qry.getPageNo(), qry.getPageSize(), page.getTotal());
    }

    @Override
    public Project getById(Long id) {
        return projectRepository.getById(id);
    }

    @Override
    public Long add(Project project) {
        Project toAddProject = new Project();
        toAddProject.setProjectName(project.getProjectName());
        toAddProject.setStatus(project.getStatus());
        projectRepository.save(toAddProject);
        return toAddProject.getId() ;
    }

    @Override
    public boolean update(Project project) {
        if (Objects.isNull(project.getId())) {
            throw new ServiceException("更新异常，请检查参数");
        }

        Project oldProject = projectRepository.getById(project.getId());
        if (Objects.isNull(oldProject)) {
            throw new ServiceException("项目不存在");
        }

        oldProject.setProjectName(project.getProjectName());
        oldProject.setStatus(project.getStatus());
        projectRepository.updateById(oldProject);
        return true;
    }

    @Override
    public boolean delete(List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            projectRepository.removeBatchByIds(ids);
        }
        return true;
    }
}
